// 数据存储
export const store = {
    set(key, data, expire) {
        try {
            if (expire) {
                expire = new Date().getTime() + expire * 1000;
            }
            window.localStorage.setItem(key, JSON.stringify({
                expire,
                data,
            }));
            return true;
        } catch (e) {
            console.error(e);
            return false;
        }
    },
    get(key) {
        try {
            const dataString = window.localStorage.getItem(key);
            if (!dataString) return null;

            const cached = JSON.parse(dataString);
            if (cached.expire && cached.expire < new Date().getTime()) {
                window.localStorage.removeItem(key);
                return null;
            }
            return cached.data;
        } catch (e) {
            console.error(e);
            return null;
        }
    },
    clear(key) {
        try {
            window.localStorage.removeItem(key);
            return true;
        } catch (e) {
            console.error(e);
            return false;
        }
    }
}
